package com.gn.controller;

import com.gn.domain.Code;
import com.gn.domain.Result;
import com.gn.domain.User;
import com.gn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    public Result insert(@RequestBody User user) {
        boolean flag = userService.save(user);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @GetMapping("/{account}/{password}")
    public Result selectByAccount(@PathVariable("account") String account, @PathVariable("password") String password) {
        if (password.equals("!!!!!")) {
            password = "";
        }
        User u = userService.select(account, password);
        Integer code = u != null ? Code.GET_OK : Code.GET_ERR;
        String message = u != null ? "" : "用户名或密码错误！";
        return new Result(u, code, message);
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        boolean flag = userService.update(user);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }
}
